package com.pai4j.common.util;

import com.google.common.base.Splitter;
import com.pai4j.common.constants.WebConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class URLUtils {

    /**
     * 从url里面获取
     *
     * @param url
     * @return
     */
    public static String getUrlHead(String url) {
        if (StringUtils.isNotBlank(url)) {
            url = url.replaceAll("www.", "");
            String[] urls = url.split("\\.");
            String headTmp = urls[0];
            return headTmp.replaceAll("http://", "").replaceAll("HTTP://", "").replace("HTTPS://", "").replaceAll
                    ("https://", "");
        }
        return null;
    }

    /**
     * @param queryString
     * @return
     */
    public static Map<String, String> getParamMap(String queryString) {
        Map<String, String> paramMap = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
        return paramMap;
    }

    /**
     * 获取链接的查询字符串参数
     * @param url
     * @return
     */
    public static Map<String, String> getParamMapFromUrl(String url) {
        int index = url.indexOf("?");
        if (index == -1) {
            return new HashMap<>();
        }
        return getParamMap(url.substring(index + 1));
    }

    public static String getUrlFromQueryParam(String url, Map<String, Object> params) {
        StringBuilder queryParam;
        if (url.endsWith(WebConstant.AND_SIGN)) {
            queryParam = new StringBuilder(url);
        } else {
            queryParam = new StringBuilder(url + WebConstant.AND_SIGN);
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {
            queryParam.append(entry.getKey()).append("=")
                    .append(entry.getValue()).append(WebConstant.AND_SIGN);
        }
        String string = queryParam.toString();
        if (string.endsWith(WebConstant.AND_SIGN)) {
            string = string.substring(0, string.length() - 1);
        }
        return string;
    }

    public static String getUrlWithQueryParam(String url, Map<String, ?> params) {
        return getUrlWithQueryParam(url, generateQueryParam(params));
    }

    public static String getUrlWithQueryParam(String url, String queryParams) {
        if (StringUtils.isNotBlank(queryParams)) {
            return url + "?" + queryParams;
        }
        return url;
    }

    public static String generateQueryParam(Map<String, ?> params) {
        StringBuilder builder = new StringBuilder();
        int index = 0;
        for (String key : params.keySet()) {
            if (params.get(key) == null) {
                continue;
            }
            if (index != 0) {
                builder.append("&");
            }
            builder.append(key);
            builder.append("=");
            builder.append(params.get(key).toString());
            ++index;
        }
        return builder.toString();
    }

    public static String removeUrlQuery(String url, String... queries) {
        int index = url.indexOf("?");
        if (index == -1) {
            return url;
        }
        Map<String, String> params = getParamMap(url.substring(index + 1));
        Map<String, String> newParams = new HashMap<>(params);
        for (String query : queries) {
            newParams.remove(query);
        }
        return getUrlWithQueryParam(url.substring(0, index), newParams);
    }

    public static String getPath(String url) {
        int start = url.indexOf("://");
        if (start == -1) {
            start = 0;
        } else {
            start = start + 3;
        }
        int middle = url.indexOf("/", start);
        if (middle == -1) {
            return "";
        }
        start = middle;
        int end = url.indexOf("?");
        if (end == -1) {
            end = url.length();
        }
        return url.substring(start, end);
    }
}
